package com.authority.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jsoup.helper.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.authority.dao.AuthOrganDAO;
import com.authority.dao.AuthUserDataDao;
import com.authority.dao.AuthUserDAO;
import com.authority.entry.AuthOrganDO;
import com.authority.entry.AuthUserDataDO;
import com.authority.entry.AuthUserDO;
import com.authority.query.OrganQuery;
import com.authority.result.PageResult;
import com.authority.service.AuthUserDataService;
import com.authority.service.UserService;
import com.authority.service.AuthOrganService;
import com.authority.vo.AuthOrganVO;
import com.suixun.dao.SchoolinfoMapper;
import com.suixun.entity.Schoolinfo;
import com.suixun.entity.SchoolinfoExample;
import com.suixun.entity.SchoolinfoExample.Criteria;
import com.suixun.util.Constant;


@Service("organService")
public class AuthOrganServiceImpl implements AuthOrganService {
	
	@Resource
	private AuthOrganDAO organDAO;
	@Resource
	private AuthUserDAO authUserDAO;
	@Resource
	private SchoolinfoMapper schoolinfoMapper;
	@Resource
	private AuthUserDataService authOrganDataService;
	@Resource
	private AuthUserDataDao authOrganDataDao;
	@Resource
	private UserService userService;
	
	@Override
	public PageResult<AuthOrganDO> queryAuthOrgan(OrganQuery queryParameter) {
		PageResult<AuthOrganDO> response = new PageResult<AuthOrganDO>();
		List<AuthOrganDO> results = new ArrayList<AuthOrganDO>();
		results = organDAO.queryAuthOrgan(queryParameter);
		response.setList(results);

		// 总条数
		int count = organDAO.queryAuthOrganCount(queryParameter);

		response.setNum(queryParameter.getNum());
		response.setTotalNum(count);
		response.setIndex(queryParameter.getIndex());
		return response;
	}

	@Override
	public Long insertAuthOrgan(AuthOrganDO authOrganDO) {
		//return organDAO.insertAuthOrgan(authOrganDO);
		return null;
	}

	@Override
	public int deleteAuthOrganByIds(String ids) {
		String[] idArr = ids.split("[,，]");
		Map<String,Object> map =new HashMap<>();
		for (String id : idArr) {
			//查询该组织下是否有账户
			map.put("organid", id);
			List<AuthUserDO> list = authUserDAO.queryUserByParam(map);
			if (list.size() > 0) {
				return -1;
			}
		}
		return organDAO.deleteAuthOrganByIds(ids);
	}

	@Override
	public int updateAuthOrgan(AuthOrganDO authOrganDO) {
		Map<String,Object> map = new HashMap<>();
		map.put("id", authOrganDO.getId());
		List<AuthOrganDO> list = organDAO.selectByParam(map);
		if (list.size() == 0) {
			return -1;
		}
		if (!StringUtil.isBlank(authOrganDO.getPrefix())) {
			String oldPrefix = list.get(0).getPrefix();
			//判断组织前缀是否已存在
			map = new HashMap<>();
			map.put("prefix", authOrganDO.getPrefix());
			list = organDAO.selectByParam(map);
			if (list.size() > 0 && !authOrganDO.getPrefix().equals(oldPrefix)) {
				return -2;
			}
			userService.updateUserAccountByOrganId(authOrganDO.getId());
		}
		int isUpdate = organDAO.updateAuthOrgan(authOrganDO);
		return isUpdate;
	}

	@Override
	public AuthOrganDO queryAuthOrganById(Long id) {
		return organDAO.queryAuthOrganById(id);
	}

	@Override
	public List<AuthOrganDO> queryAllAuthOrgan() {
		return organDAO.queryAllAuthOrgan();
	}

	@Override
	public List<AuthOrganDO> queryActivityAuthOrgan() {
		return organDAO.queryActivityAuthOrgan();
	}

	@Override
	public int updateAuthOrganStatus(String ids, int status, Long operatorId) {
		return organDAO.updateAuthOrganStatus(ids, status, operatorId);
	}

	@Override
	public List<AuthOrganDO> queryAuthOrganDOList(Map<String,Object> map) {
		return organDAO.queryAuthOrganDOList(map);
	}
}
